package com.jinmei.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jinmei.comparator.WorkScheduleDTOComparator;
import com.jinmei.model.WorkSchedule;
import com.jinmei.service.WorkScheduleService;
import com.jinmei.utils.JsonResponseUtil;
import com.jinmei.utils.WorkScheduleUtil;
import com.jinmei.web.CalendarTask;
import com.jinmei.web.WorkScheduleDTO;


public class WorkScheduleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(WorkScheduleAction.class);

	private WorkScheduleService workScheduleService;
	
	private String startDate;
	
	private String endDate;
	
	private String workingDate;
	
	private String workingType;

	public String execute() throws Exception {
		//List<WorkSchedule> workSchedules = workScheduleService.getWorkSchedulesByDate(startDate, endDate);
		setUserSessionIdAndUsername();
		return SUCCESS;
	}
	
	public void findWorkSchedule() throws Exception {
		logger.info("startDate: " + startDate);
		logger.info("endDate: " + endDate);
		List<WorkSchedule> workSchedules = workScheduleService.getWorkSchedulesByDate(startDate, endDate);
		
		List<CalendarTask> tasks = WorkScheduleUtil.convertCalendarTasks(workSchedules);
		
		JsonResponseUtil.writeJsonArray(tasks);
	}

	public void findWorkScheduleByDayType() throws Exception {
		logger.info("findWorkScheduleByDayType method call");
		logger.info("workingDate: " + workingDate);
		logger.info("workingType: " + workingType);
		
		List<WorkSchedule> workSchedules = workScheduleService.getWorkSchedulesByDateAndType(workingDate, workingType);
		List<WorkScheduleDTO> dtos = new ArrayList<WorkScheduleDTO>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		for(WorkSchedule workSchedule : workSchedules){
			WorkScheduleDTO dto = new WorkScheduleDTO();
			dto.setId(workSchedule.getId());
			dto.setEmployeeId(workSchedule.getEmployee().getfNumber());
			dto.setEmployeeName(workSchedule.getEmployee().getfName());
			dto.setWorkingType(workSchedule.getWorkingType());
			dto.setWorkDate(sdf.format(workSchedule.getWorkDate()));
			dtos.add(dto);
		}
		
		java.util.Collections.sort(dtos, new WorkScheduleDTOComparator());
		JsonResponseUtil.writeJsonArray(dtos);
		
	}
	public WorkScheduleService getWorkScheduleService() {
		return workScheduleService;
	}

	public void setWorkScheduleService(WorkScheduleService workScheduleService) {
		this.workScheduleService = workScheduleService;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getWorkingDate() {
		return workingDate;
	}

	public void setWorkingDate(String workingDate) {
		this.workingDate = workingDate;
	}

	public String getWorkingType() {
		return workingType;
	}

	public void setWorkingType(String workingType) {
		this.workingType = workingType;
	}  

}
